package com.mdd.front.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mdd.common.entity.Region;
import com.mdd.common.mapper.RegionMapper;
import com.mdd.front.service.StoreFrontService;
import com.mdd.front.vo.region.RegionDetailVo;
import com.mdd.front.vo.store.StoreHistoryVo;
import com.mdd.front.vo.store.StoreVo;
import com.mdd.store.entity.Store;
import com.mdd.store.mapper.StoreMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 *
 *
 * {@code @Description}
 * {@code @Author} 幻速
 * {@code @Date} 2023/11/1 19:05
 **/
@Service
public class StoreFrontServiceImpl implements StoreFrontService {
    @Resource
    StoreMapper storeMapper;
    @Resource
    RegionMapper regionMapper;
    @Override
    public StoreVo storeInfo(Long storeId) {
        Store model = storeMapper.selectById(storeId);

        Assert.notNull(model, "门店数据不存在");

        StoreVo vo = new StoreVo();
        BeanUtils.copyProperties(model, vo);
        Region province = regionMapper.selectById(model.getProvince());
        Region city = regionMapper.selectById(model.getCity());
        Region country = regionMapper.selectById(model.getCounty());
        RegionDetailVo provinceVo  =new RegionDetailVo();
        RegionDetailVo cityVo  =new RegionDetailVo();
        RegionDetailVo countryVo  =new RegionDetailVo();

        BeanUtils.copyProperties(province, provinceVo);
        BeanUtils.copyProperties(city, cityVo);
        BeanUtils.copyProperties(country, countryVo);
        vo.setProvince(provinceVo);
        vo.setCity(cityVo);
        vo.setCounty(countryVo);
        return vo;
    }

    @Override
    public List<StoreHistoryVo> historyStore(Long userId) {
        List<StoreHistoryVo> storeVos=new ArrayList<>();
        storeMapper.historyStore(userId).forEach(store -> {
            StoreHistoryVo storeHistoryVo=new StoreHistoryVo();
            BeanUtils.copyProperties(store,storeHistoryVo);
            storeVos.add(storeHistoryVo);
        });
        return storeVos;
    }

    @Override
    public boolean isExists(String storeId) {
        LambdaQueryWrapper<Store> storeLambdaQueryWrapper=new LambdaQueryWrapper<>();
        storeLambdaQueryWrapper.eq(Store::getId,storeId);
        return storeMapper.exists(storeLambdaQueryWrapper);
    }

    @Override
    public Object getDeptCode(String storeId) {
        return storeMapper.selectById(storeId).getDeptCode();
    }
}
